#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include "network.h"
#include "cam_fetch.h"

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[]) {
  int sockfd, portno, n;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  char c;
  message_t msg;

  char buffer[256];
  if (argc < 3) {
     fprintf(stderr,"usage %s hostname port\n", argv[0]);
     exit(0);
  }
  portno = atoi(argv[2]);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) 
      error("ERROR opening socket");
  server = gethostbyname(argv[1]);
  if (server == NULL) {
      fprintf(stderr,"ERROR, no such host\n");
      exit(0);
  }
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
  serv_addr.sin_port = htons(portno);
  if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
      error("ERROR connecting");
  while (1) {
    printf("Enter command: ");
    c = toupper(getchar());
    switch(c) {
      case 'C': //camera settings
        memset(&msg, 0, sizeof(msg));
        printf("Enter the flag bitset: ");
        scanf("%d", &msg.payload.cam_settings.flags);
        printf("Enter the rate: ");
        scanf("%d", &msg.payload.cam_settings.rate);
        printf("Enter the camerea URL with all options already encoded: ");
        scanf("%1024s", &msg.payload.cam_settings.url);
        n = write(sockfd, &msg, sizeof(msg));
        if(n < 0)
          error("ERROR writing to the socket\n");
        break;
      case 'F': //ftp settings
        memset(&msg, 0, sizeof(msg));
        printf("Enter the flag bitset: ");
        scanf("%d", &msg.payload.ftp_settings.flags);
        printf("Enter the full FTP URL with all subdirs: ");
        scanf("%128s", &msg.payload.ftp_settings.destination);
        n = write(sockfd, &msg, sizeof(msg));
        if(n<0)
          error("ERROR writing to the socket\n");
        break;
      case 'H': //help
        printf("Commands:\n"
               "  H - Prints this help menu\n"
               "  C - Set camera settings\n"
               "  Q - Quit\n");
        break;
      case 'Q': //quit
        return 0;
        break;
      default:
        printf("Invalid option, try the (H)elp\n");
        break;
    }

    while(getchar() != '\n'); //eat the new line off the buffer and any dead input
  }  
/*  printf("Please enter the message: ");
  bzero(buffer,256);
  fgets(buffer,255,stdin);
  msg.type = MSG_CAM_SETTINGS;
  strncpy(&settings.url, "lava rocks this", 1024);
  settings.flags = 0;
  settings.rate = 1024;
  memcpy(&msg.payload, &settings, sizeof(settings));
  n = write(sockfd,&msg, sizeof(msg));//buffer,strlen(buffer));
  if (n < 0) 
    error("ERROR writing to socket");
  bzero(buffer,256);
  n = read(sockfd,buffer,255);
  if (n < 0) 
    error("ERROR reading from socket");
  printf("%s\n",buffer);
  close(sockfd);*/
  return 0;
}
